package com.door.erp.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Expenditure;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ExpenditureService;

/**
 *类说明
 *
 * @author Xuelin Yang E-mail:yangxuelin@foxmail.com
 * @version 创建时间：2013-6-3 上午9:45:46
 */
@Controller
public class ExpenditureController {
	
	@Resource(name="expenditureService", type=ExpenditureService.class)
	private ExpenditureService expenditureService;
	
	@RequestMapping("/expenditure/expenditures.do")
	public ModelAndView expenditures(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = expenditureService.getCount(params);
		pager.setTotalCount(totalCount);
		List<Expenditure> expenditures = expenditureService.getAll(params, pager);

		ModelAndView mav = new ModelAndView();
		mav.setViewName("expenditure/expenditures");
		mav.addObject("pager", pager);
		mav.addObject("expenditures", expenditures);
		return mav;
	}
	
	@RequestMapping("/expenditure/addPage.do")
	public ModelAndView getAddPage() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("expenditure/add");
		return mav;
	}
	
	@RequestMapping("/expenditure/add.do")
	public ModelAndView add(Expenditure expenditure) {
		expenditure.setStatus(BaseInfoStatus.NORMAL);
		expenditure.setCreateTime(new Date());
		expenditureService.add(expenditure);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("expenditure");
		resonse.setMessage("添加开销记录成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/expenditure/getExpenditure.do")
	public ModelAndView getExpenditure(@RequestParam(value="id", required=true) Long id) {
		Expenditure expenditure = expenditureService.getObject(id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("expenditure/modify");
		mav.addObject("expenditure", expenditure);
		return mav;
	}
	
	@RequestMapping("/expenditure/modify.do")
	public ModelAndView modify(Expenditure expenditure) {
		expenditureService.modify(expenditure);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("expenditure");
		resonse.setMessage("修改开销记录成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/expenditure/delete.do")
	public ModelAndView delete(long id) {
		Expenditure expenditure = expenditureService.getObject(id);
		expenditure.setStatus(BaseInfoStatus.DELETED);
		expenditureService.modify(expenditure);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("expenditure");
		resonse.setMessage("删除开销记录成功");
		resonse.setCallbackType("");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/expenditure/exportPage.do")
	public String exportPage(HttpServletRequest request) {
		return "expenditure/export";
	}
	
	@RequestMapping("/expenditure/export.do")
	public void export(HttpServletRequest request, HttpServletResponse response) {
		String beginTimeStr = request.getParameter("beginTime");
		String endTimeStr = request.getParameter("endTime");
		String employeeStr = request.getParameter("employee.id");
		String typeStr = request.getParameter("type.id");

		OutputStream os = null;
		//进行转码，使其支持中文文件名  
		String fileName = "expenditure";
        try {
			fileName = java.net.URLEncoder.encode("开销报表", "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			fileName = "expenditure";
			e1.printStackTrace();
		}  
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet();  
		
		HSSFCellStyle hcs = workbook.createCellStyle();
		hcs.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		hcs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		//设置列宽
		sheet.setColumnWidth(0, 2000);
		sheet.setColumnWidth(1, 5000);
		sheet.setColumnWidth(2, 6000);
		sheet.setColumnWidth(3, 10000);
		sheet.setColumnWidth(4, 4000);
		sheet.setColumnWidth(5, 6000);
		sheet.setColumnWidth(6, 7000);
		//表头
		HSSFRow head = sheet.createRow(0);
		head.setHeight((short)400);
		
		HSSFCell num = head.createCell(0);
		num.setCellType(HSSFCell.CELL_TYPE_STRING);
		num.setCellValue("序号");
        HSSFCell employeeName = head.createCell(1);
        employeeName.setCellType(HSSFCell.CELL_TYPE_STRING);
        employeeName.setCellValue("员工");
        HSSFCell expenditureType = head.createCell(2);
        expenditureType.setCellType(HSSFCell.CELL_TYPE_STRING);
        expenditureType.setCellValue("开销类型");
        HSSFCell reason = head.createCell(3);
        reason.setCellType(HSSFCell.CELL_TYPE_STRING);
        reason.setCellValue("原因");
        HSSFCell money = head.createCell(4);
        money.setCellType(HSSFCell.CELL_TYPE_STRING);
        money.setCellValue("金额");
        HSSFCell remark = head.createCell(5);
        remark.setCellType(HSSFCell.CELL_TYPE_STRING);
        remark.setCellValue("备注");
        HSSFCell createTime = head.createCell(6);
        createTime.setCellType(HSSFCell.CELL_TYPE_STRING);
        createTime.setCellValue("创建时间");
        //查询出数据
        List<Expenditure> expenditures = expenditureService.getExpenditureByTimeAndType(beginTimeStr, endTimeStr, typeStr, employeeStr);
		if(expenditures == null) {
			try {
				os = response.getOutputStream();
				workbook.write(os);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		else {
			int i = 1;
	        HSSFRow row = null;
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        
			for(Expenditure ex : expenditures) {
				row = sheet.createRow(i);
				row.setHeight((short)400);
	        	
	        	num = row.createCell(0);
	        	num.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	num.setCellValue(String.valueOf(i));
	        	num.setCellStyle(hcs);
	        	
	        	employeeName = row.createCell(1);
	        	employeeName.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	employeeName.setCellValue(ex.getEmployee().getName());
	        	employeeName.setCellStyle(hcs);
	        	
	        	expenditureType = row.createCell(2);
	        	expenditureType.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	expenditureType.setCellValue(ex.getType().getDescription());
	        	expenditureType.setCellStyle(hcs);
	        	
	        	reason = row.createCell(3);
	        	reason.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	reason.setCellValue(ex.getReason());
	        	reason.setCellStyle(hcs);
	        	
	        	money = row.createCell(4);
	        	money.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	money.setCellValue(ex.getMoney());
	        	money.setCellStyle(hcs);
	        	
	        	remark = row.createCell(5);
	        	remark.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	remark.setCellValue(ex.getRemark());
	        	remark.setCellStyle(hcs);
	        	
	        	createTime = row.createCell(6);
	        	createTime.setCellType(HSSFCell.CELL_TYPE_STRING);
	        	createTime.setCellValue(format.format(ex.getCreateTime()));
	        	createTime.setCellStyle(hcs);
	        	
	        	i++;
			}
			try {
				os = response.getOutputStream();
				workbook.write(os);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
        }
	}

}
